package PageRank;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.input.KeyValueTextInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;

public class DriTest {
    public static final String groupName = "myGroup";
    public static final String counterName = "myCounter";

    public static void main(String[] args) throws Exception {
        //获取配置类对象
        Configuration conf = new Configuration();
        //用于记录运行的轮数,表示第几轮运行mr程序
        int runCount = 0;
        //定义一个标准,如果满足这个标准,则认为各个页面的pr值趋于稳定,可以停止mr程序的运算
        double d = 0.0000001;
        while (true) {
            runCount++;
            conf.setInt("runCount", runCount);
            Job job = Job.getInstance(conf);
            job.setJarByClass(DriTest.class);
            job.setMapperClass(MapTest.class);
            job.setReducerClass(RedTest.class);
            job.setMapOutputKeyClass(Text.class);
            job.setMapOutputValueClass(Text.class);
            job.setOutputKeyClass(Text.class);
            job.setOutputValueClass(Text.class);
            /** 设置采用KeyValueTextInputFormat来读取数据封装kv对送到mapper的map方法会根据tab分割数据,把分割后的第一个数据当做key,其它当做value*/
            job.setInputFormatClass(KeyValueTextInputFormat.class);
            Path path;
            if (runCount > 1) {
                path = new Path("D:\\MP\\PageRank\\output" + (runCount - 1));
            } else {
                path = new Path("D:\\MP\\PageRank\\input");
            }
            FileInputFormat.setInputPaths(job, path);
            Path out = new Path("D:\\MP\\PageRank\\output" + runCount);
            if (out.getFileSystem(conf).exists(out)) {
                out.getFileSystem(conf).delete(out, true);
            }
            FileOutputFormat.setOutputPath(job, out);
            boolean b = job.waitForCompletion(true);
            if (b) {
                long value = job.getCounters().findCounter(DriTest.groupName, DriTest.counterName).getValue();

                //求4个页面平均与上轮pr值的差值,这个地方要加.0
                double diff = value / 4000.0;
                //输出目前的精度
                System.out.println("diff is " + diff);
                //满足条件的情况下,则认为是页面的pr值趋于稳定,可以退出循环,不再提交mr程序
                if (diff < d) {
                    break;
                }
            }
        }
    }
}
